<?php
namespace addons\baiduSpider\controller;

use think\Request;
use think\Loader;

use app\common\controller\AdminBase;

class ApiAdminBaiduSpider extends AdminBase
{

    public function itemList(Request $request)
    {
      	$page = input('post.page');
		$limit = input('post.limit');
		$orderBy = input('post.orderBy');
		$order = input('post.order');
		if (!$page) {
		  $page = 1;
		}
		if (!$limit) {
            $limit = 10;
		}
		if (!$orderBy) {
            $orderBy = 'add_time';
		}
		if (!$order) {
			$order = 'desc';
		}
	    $spidersList = db('AddonsSpiders')->limit($limit)->page($page)->order($orderBy, $order)->select();
//          foreach ($spidersList as $k => $v) {
//              $ip = @file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=Json&ip=".$v['ua']);
//              $spidersList[$k]['ip'] = json_decode($ip,true);
//          }
	    return jsonSuccess('',['spidersList' => $spidersList,'total' => db('AddonsSpiders')->count(),'page' => $page]);
    }

    public function spidersToday()
    {
        $type = input('post.type');
        $type ? $type : $type = 'pc';
        $todyStartTime = strtotime(date('Y-m-d'));
        $thisDaySeconds = 24 * 60 *60;
        for ($i = 0; $i < 25; $i++) {
            $spidersTodayList[] = db('AddonsSpiders')->where('add_time','between',[$todyStartTime + ($i * 3600) ,$todyStartTime+(($i + 1) * 3600)])->where('type',$type)->count();
        }
        return jsonSuccess('',['spidersTodayList' => $spidersTodayList]);
    }

    public function getCount()
    {
        $todayUserCount = db('AddonsSpiders')->where('add_time','>',strtotime(date('Y-m-d')))->count();
        $yesterdayUserCount = db('AddonsSpiders')->where('add_time','>',strtotime(date('Y-m-d'))-(60*60*24*1))->where('add_time','<',strtotime(date('Y-m-d')))->count();
        $weekUserCount = db('AddonsSpiders')->where('add_time','>',strtotime(date('Y-m-d'))-(60*60*24*7))->where('add_time','<',strtotime(date('Y-m-d')))->count();
        $allUserCount = db('AddonsSpiders')->count();
        return jsonSuccess('',['todayUserCount' => $todayUserCount,'yesterdayUserCount' => $yesterdayUserCount, 'weekUserCount'=> $weekUserCount, 'allUserCount' => $allUserCount] );

    }

    

}